﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obra.SmugMug.Api;
using System.Management.Automation.Provider;

namespace Obra.PowerShell.SmugMug
{
    internal class PathMappingServiceProvider
        : IPathMappingService
    {
        private readonly string root;
        private readonly CmdletProvider cmdletProvider;
        private readonly ISmugMugClientService smugMugClient;

        private const int DRIVE = 1;
        private const int ALBUM = 2;
        private const int IMAGE = 3;

        /// <summary>
        /// c-tor
        /// </summary>
        public PathMappingServiceProvider(
            string root,
            CmdletProvider cmdletProvider,
            ISmugMugClientService smugMugClient)
        {
            this.root = root;
            this.cmdletProvider = cmdletProvider;
            this.smugMugClient = smugMugClient;
        }

        private PathSegments GetPathSegments(string path)
        {
            PathSegments segments = new PathSegments(path.Replace(":", "").Replace(@"\\", @"\"));
            return segments;
        }

        #region IPathMappingService Members

        public ICmdletService ToCmdletService(string path)
        {
            PathSegments segments = this.GetPathSegments(path);

            switch (segments.Count)
            {
                case PathMappingServiceProvider.DRIVE:
                    return new DriveCmdletServiceProvider(
                        segments,
                        this.smugMugClient,
                        this);
                case PathMappingServiceProvider.ALBUM:
                    return new AlbumCmdletServiceProvider(
                        segments,
                        this.smugMugClient,
                        this);
                case PathMappingServiceProvider.IMAGE:
                    return new ImageCmdletServiceProvider(
                        segments,
                        this.smugMugClient,
                        this);
                default:
                    string message = String.Format(Resource.ErrorPathTooManySegments,
                        path);

                    throw new ArgumentException(message);
            }
        }

        #endregion
    }
}
